package com.cali.libcore.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cali.common.kt.create
import com.cali.common.kt.toast
import com.cali.common.kt.unregister
import com.cali.libcore.R
import com.cali.libcore.http.BaseResult
import com.cali.libcore.view.ToolbarHelper
import com.cali.libcore.view.dialog.LoadingDialog
import kotlinx.android.synthetic.main.loading_my_dialog.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import me.yokeyword.fragmentation.SupportFragment

abstract class BaseFragment : SupportFragment(), IToolbarView, IFragment,
    CoroutineScope by MainScope() {

    protected val loadingDialog :LoadingDialog by lazy {
        LoadingDialog.Builder(context)
            .setMessage("正在登录")
            .create()
    }

    protected fun showTip(tip: String) {
        loadingDialog.tipTextView.text = tip
        if (!isResumed) {
            loadingDialog.show()
        }
    }

    protected fun dismissTip() {
        loadingDialog.dismiss()
    }

    protected lateinit var mContext: Context

    protected val currentActivity by lazy {
        requireActivity()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context.applicationContext
    }

    var toolbarHelper: ToolbarHelper? = null

    final override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.create(getLayoutId())
        return if (hasToolbar()) {
            toolbarHelper = ToolbarHelper(view.context, view).apply {
                if (hasBackIcon()) {
                    toolbar.apply {
                        setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24)
                        setNavigationOnClickListener {
                            pop()
                        }
                    }
                }
            }
            toolbarHelper?.contentView
        } else {
            view
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(savedInstanceState)
    }

    override fun hasBackIcon() = true

    override fun hasToolbar() = false


    fun startParentParentBrotherFragment(fragment: SupportFragment) {
        val mainFragment = parentFragment?.parentFragment as? SupportFragment
        mainFragment?.start(fragment, SINGLETASK)
    }


    fun startParentBrotherFragment(fragment: SupportFragment) {
        val mainFragment = parentFragment as? SupportFragment
        mainFragment?.start(fragment, SINGLETASK)
    }

    fun <T> error(result: BaseResult<T>? = null) {
        context?.toast(result?.message ?: "请求失败！")
    }

    fun toast(msg: String) {
        context?.toast(msg)
    }

    override fun onDestroy() {
        super.onDestroy()
        this.unregister()
        cancel()
    }

}